<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/28
 * Time: 17:00
 * 好友关系
 */

namespace app\api\controller\Index;


use app\api\controller\AppController;
use app\api\model\Sns\SnsModel;
use think\Db;
use app\Utils\Util;
use think\Request;

class Sns extends AppController
{
    /**
     * @param Request $request
     * @return array
     * 关注 /取消
     * $type 1 关注  0 取消
     * sns_tomid  视频id
     * $sns_followstate  1 单方面关注  2 相互关注
     */
    public function follow(Request $request)
    {
        $sns_followstate = (int)$request->param('sns_followstate', 1);
        $type = (int)$request->param('type', 1);
        $sns_tomid = (int)$request->param('sns_tomid');
        if (empty($sns_tomid)) return Util::returnJson(self::CODE_ERROR, null, '作者必传');
        if (!$user = $this->user) return Util::returnJson(self::CODE_LOGIN, null, "token已过期");
        if ($type == 1) {
            if (!SnsModel::where(['sns_frommid' => $user['id'], 'sns_tomid' => $sns_tomid, 'sns_followstate' => $sns_followstate])->find()) {
                SnsModel::create(['sns_frommid' => $user['id'], 'sns_tomid' => $sns_tomid, 'sns_followstate' => $sns_followstate, 'sns_addtime' => time()]);
            }
        } else {
            SnsModel::where(['sns_frommid' => $user['id'], 'sns_tomid' => $sns_tomid, 'sns_followstate' => $sns_followstate])->delete();
        }
        return Util::returnJson(self::CODE_SUCCESS, null, $type == 1 ? '关注成功' : '取消成功');
    }
}